package com.smbms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smbms.pojo.Role;
import com.smbms.pojo.User;
import com.smbms.service.RoleService;
import com.smbms.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;


    @RequestMapping(value = "/list.html")
    public String list(Model model
            ,@RequestParam(value = "pageIndex",required = false) Integer currPageNo
            ,@RequestParam(value = "queryname",required = false) String queryname
            ,@RequestParam(value = "queryUserRole",required = false) Integer queryUserRole){

        //页面大小
        int pageSize = 6;
        //当前页码
        if(currPageNo==null){
            currPageNo = 1;
        }

        if (queryUserRole == null){
            queryUserRole = 0;
        }

        List<Role> roleList = roleService.getRoleList();


        IPage<User> iPageUser = userService.getUserList(queryname,queryUserRole,currPageNo,pageSize);

        List<User> userList = iPageUser.getRecords();

        int totalCount = (int) iPageUser.getTotal();

        //总页数
        int totalPageCount = (int) iPageUser.getPages();

        model.addAttribute("userList",userList);
        model.addAttribute("roleList",roleList);
        model.addAttribute("queryUserName",queryname);
        model.addAttribute("queryUserRole",queryUserRole);

        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currPageNo);
        return "userlist";
    }
}
